package com.mojian.controller.recommendation;

import cn.dev33.satoken.stp.StpUtil;
import com.mojian.common.Result;
import com.mojian.service.RecommendationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/protal/recommendation")
@RequiredArgsConstructor
@Api(tags = "门户-推荐系统")
@Slf4j
public class RecommendationController {

    private final RecommendationService recommendationService;

    @GetMapping("/personalized")
    @ApiOperation(value = "获取个性化推荐文章")
    public Result<Map<String, Object>> getPersonalizedRecommendations() {
        try {
            // 检查用户是否登录
            if (!StpUtil.isLogin()) {
                return Result.error("用户未登录，无法获取个性化推荐");
            }
            
            Long userId = StpUtil.getLoginIdAsLong();
            Map<String, Object> recommendations = recommendationService.getPersonalizedRecommendations(userId);
            return Result.success(recommendations);
        } catch (Exception e) {
            log.error("获取个性化推荐文章失败", e);
            return Result.error("获取个性化推荐失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/userAnalysis")
    @ApiOperation(value = "获取用户浏览数据分析")
    public Result<Map<String, Object>> getUserBrowsingAnalysis() {
        try {
            // 检查用户是否登录
            if (!StpUtil.isLogin()) {
                return Result.error("用户未登录，无法获取数据分析");
            }
            
            Long userId = StpUtil.getLoginIdAsLong();
            Map<String, Object> analysis = recommendationService.getUserBrowsingAnalysis(userId);
            return Result.success(analysis);
        } catch (Exception e) {
            log.error("获取用户浏览数据分析失败", e);
            return Result.error("获取用户浏览数据分析失败: " + e.getMessage());
        }
    }
} 